<?php
include_once('coneccion.php');

$db = DataBase :: getInstance();

if($_REQUEST['action'] == 'add'){
	
	$db->setQuery("insert into entries (title,entry) values ('".$_REQUEST['title']."', '".$_REQUEST['entry']."')");
	$res = $db->execute();
	
	$db->setQuery("SELECT MAX(id) id FROM entries");
	$res = $db->loadObjectList();
	
	$test = array('success' => 'success', 'id' => $res[0]->id);
	
	echo json_encode($test);

}else{

/*$test = array(
			  'users' => array(
							   'title' => 'asd',
							   'entry' => 'owqp'
							   )
			  );

$test2 = array();

$test2['users'][0] = array('title' => 'asd',
							   'entry' => 'owqp');
$test2['users'][1] = array('title' => 'asd',
							   'entry' => 'owqp');
*/

$db->setQuery("select * from entries");
$res = $db->loadObjectList();

$entries = array();

foreach($res as $k => $e){
	$entries[$k] = array(
						 'id' => $e->id,
						 'title' => $e->title,
						 'entry' => $e->entry
						 );
}


echo json_encode($entries);
}
?>